﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AGRTraining.za.co.agrtraining.model;

namespace AGRTraining.za.co.agrtraining.repository.Impl
{
    public class QualificationDAO : IDAO<Qualification>
    {
        private AGRModelContainer agrDb = new AGRModelContainer();

        public void persist(Qualification entity)
        {
            agrDb.Qualifications.AddObject(entity);
        }

        public void remove(Qualification entity)
        {
            agrDb.Qualifications.DeleteObject(entity);
        }

        public void removeById(string code)
        {
            foreach (Qualification qualification in agrDb.Qualifications.Where(s => s.QualificationCode == code))
            {
                agrDb.Qualifications.DeleteObject(qualification);
            }
        }

        public void merge(Qualification entity)
        {
            foreach (Qualification qualification in agrDb.Qualifications.Where(s => s.QualificationCode == entity.QualificationCode))
            {
                qualification.QualificationDescription = entity.QualificationDescription;
                qualification.QualificationType = entity.QualificationType;
            }

            //Select ALL
            //var students = (from s in agrDb.Students
            //                where s.StudentCode == "dsfsdf"
            //                select s);

            //Select Certain Columns
            //var students2 = (from s in agrDb.Students
            //                select new
            //                {
            //                    s.StudentCourses,
            //                    s.StudentDisability
            //                });

            //Order rows
            //var orderedListByCourses = students.OrderBy(s => s.StudentCourses);

        }

        public Qualification findById(string code)
        {
            Qualification qualification = new Qualification();

            foreach (Qualification qualify in agrDb.Qualifications.Where(s => s.QualificationCode == code))
            {
                qualification = qualify;
            }

            return qualification;
        }

        public IEnumerable<Qualification> findAll()
        {
            var qualifications = (from qualify in agrDb.Qualifications
                            select qualify);

            return qualifications;
        }

        public IEnumerable<Qualification> findInRange(int firstResult, int maxResuslts)
        {
            var qualifications = (from qualify in agrDb.Qualifications
                            select qualify);

            return qualifications.Skip(firstResult).Take(maxResuslts);
        }

        public long count()
        {
            var qualifications = (from qualify in agrDb.Qualifications
                            select qualify).ToList();

            return qualifications.Count();
        }

        public void commit()
        {
            try
            {
                agrDb.SaveChanges();
            }
            catch
            {

            }
        }
    }
}